const faker = require('faker/locale/zh_CN')
const knex = require('./libs/knex')

// id 为数据库最大id数 limit 是插入多少条 仅适用于本地测试生成假数据

class Seeder {
    constructor() {
        this.$knex = knex
        // this.product()
        // this.partners()
        // this.packages()
        // this.boxes()
        // this.packages_products()
        return this
    }
    async product() {
        // 构造条数
        let res= await this.$knex('products').select('id').orderBy('id', 'DESC').limit(1)
        let id
        if (res.length > 0) {
            id = res[0].id
        } else {
            id = 1
        }
        let limit = id + 10;
        let data = []
        let origin_country = ['US', 'CN', 'JP', 'KR']
        while(id < limit) {
            data.push({
                'partner_id': 8,
                'description': faker.name.findName(),
                'price': faker.random.number({
                    min:10,
                    max:100,
                    precision: 0.01
                }),
                'weight': faker.random.number({
                    min:1.2,
                    max:10.0,
                    precision: 0.01
                }),
                'origin_country': faker.random.arrayElement(origin_country),
                'created': Math.round(new Date().getTime() /1000)
            })
            id++
        }
        let insert = await this.$knex('products').insert(data)
        console.log(`插入商品数据${data.length} 条`)                
        console.log(insert)
    }

    async partners() {
        // 构造条数
        let res= await this.$knex('partners').select('id').orderBy('id', 'DESC').limit(1)
        let id
        if (res.length > 0) {
            id = res[0].id
        } else {
            id = 1
        }
        let limit = id + 10
        
        let data = []
        let pad = '000000000000'
        let origin_country = ['US', 'CN', 'JP', 'KR']
        console.log(`开始插入id:${id}`)        
        while(id < limit) {
            data.push({
                'agent_id': 1,
                'name': faker.name.findName(),
                'code': `LPR${pad.substr(id.toString().length -1)}${id}`,
                'contact': faker.name.findName(),
                'mobile': faker.phone.phoneNumber(),
                'type': 1,
                'appkey': faker.random.uuid(),
                'secret': faker.random.uuid(),
                'balance': faker.random.number({
                    min:1,
                    max:1000,
                    precision: 0.01
                }) ,
                'status': 0,
                'created': Math.round(new Date().getTime() /1000)
                
            })
            id++
        }        
        let insert = await this.$knex('partners').insert(data)
        console.log(`插入商家数据${data.length} 条`)                
        console.log(insert)
    }
    async packages() {
        // 构造条数
        let res= await this.$knex('packages').select('id').orderBy('id', 'DESC').limit(1)
        let id
        if (res.length > 0) {
            id = res[0].id
        } else {
            id = 1
        }
        let limit = id + 10

        let pad = '000000000000'        
        let data = []
        let origin_country = ['US', 'CN', 'JP', 'KR']
        while(id < limit) {
            let name = faker.name.findName()
            data.push({
                'package_code'           :`LPK${pad.substr(id.toString().length -1)}${id}`,
                'partner_id'             :8,
                'erp_oid'                :id,
                'box_id'                 :1,
                'partner_service_id'     :1,
                'receiver_firstname'     :name,
                'receiver_lastname'      :name,
                'receiver_mobile'        :faker.phone.phoneNumber('13#########'),
                'receiver_country'       :'中国',
                'receiver_state'         :faker.address.state(),
                'receiver_city'          :faker.address.city(),
                'receiver_address_line1' :faker.address.streetAddress(),
                'receiver_address_line2' :faker.address.secondaryAddress() ,
                'receiver_zipcode'       :faker.address.zipCode(),
                'weight_report'          :faker.random.number({
                    min:1,
                    max:10,
                    precision: 0.01
                }) ,
                'width_report'           :faker.random.number({
                    min:1,
                    max:10,
                    precision: 0.01
                }),
                'length_report'          :faker.random.number({
                    min:1,
                    max:10,
                    precision: 0.01
                }) ,
                'height_report'          :faker.random.number({
                    min:1,
                    max:10,
                    precision: 0.01
                }),
                'created': Math.round(new Date().getTime() /1000)
                
            })
            id++
        }
        let insert = await this.$knex('packages').insert(data)
        console.log(`插入包裹数据${data.length} 条`)                
        console.log(insert)
    }
    async boxes() {
        // 构造条数
        let res= await this.$knex('boxes').select('id').orderBy('id', 'DESC').limit(1)
        let id
        if (res.length > 0) {
            id = res[0].id
        } else {
            id = 1
        }
        let limit = id + 10

        let data = []
        let pad = '000000000000'        
        let origin_country = ['US', 'CN', 'JP', 'KR']
        while(id < limit) {
            data.push({
                'partner_id': 8,
                'warehouse_id': 1,
                'box_code': `LBX${pad.substr(id.toString().length -1)}${id}`,
                'feeder_type': 1,
                'booking_time': Math.round(new Date().getTime() /1000),
                'fee_report': faker.random.number({
                    min:10,
                    max:100,
                    precision: 0.01
                }),
                'status': 0,
                'status_pay': 0,
                'created': Math.round(new Date().getTime() /1000)
            })
            id++
        }
        let insert = await this.$knex('boxes').insert(data)
        console.log(`插入集运箱数据${data.length} 条`)                
        console.log(insert)
    }

    // 创建packages_products
    async packages_products() {

        let res= await this.$knex('packages_products').select('package_id').orderBy('id', 'DESC').limit(1)
        let package_id
        if (res.length > 0) {
            package_id = res[0].package_id
        } else {
            package_id = 1
        }

        let packages = await this.$knex('packages').select('id').where('id', '>', package_id).limit(1000)
        let products = await this.$knex('products').select('id').limit(1000)
       
        let packages_ids = []
        let product_ids = []
        
        packages.forEach(function(element) {
            packages_ids.push(element.id)
        }, this);
        products.forEach(function(element) {
            product_ids.push(element.id)
        }, this);

        let data = []
        for (let key in packages_ids) {
           data.push({
               'package_id': packages_ids[key],
               'product_id': faker.random.arrayElement(product_ids),
               'amount': faker.random.number({
                   min:1,
                   max:5
               }),
               'created': Math.round(new Date().getTime() /1000)
               
           })
           if ((key % 3) ==1) {
                data.push({
                    'package_id': packages_ids[key],
                    'product_id': faker.random.arrayElement(product_ids),
                    'amount': faker.random.number({
                        min:1,
                        max:3
                    }),
                    'created': Math.round(new Date().getTime() /1000)
                    
                })
           }
        }

        // console.log(data)
        // console.log(packages_ids)
        // console.log(product_ids)
        let insert = await this.$knex('packages_products').insert(data)
        console.log(`插入packages_products数据${data.length} 条`)                
        console.log(insert)
        
    }
}

const S = new Seeder()